package main

import (
	"context"
	pb "fgo/advance/grpc/grpc-gateway/proto/hello_http"
	"fmt"
	"google.golang.org/grpc"
	"log"
	"net"
)

// 定义helloHttpService并实现约定的接口
type helloHttpService struct{}

// HelloHttpService ...
var HelloHttpService = helloHttpService{}

func (h helloHttpService) SayHello(ctx context.Context, in *pb.HelloHttpRequest) (*pb.HelloHttpReply, error) {
	resp := new(pb.HelloHttpReply)
	resp.Message = "Hello " + in.Name + "."

	return resp, nil
}

const port = 9090

func main() {
	lis, err := net.Listen("tcp", fmt.Sprintf(":%d", port))
	if err != nil {
		log.Fatalf("failed to listen: %v", err)
	}
	s := grpc.NewServer()
	pb.RegisterHelloHttpServer(s, &helloHttpService{})
	log.Println(fmt.Sprintf("Listening and serving TCP on: 0.0.0.0:%d", port))
	if err := s.Serve(lis); err != nil {
		log.Fatalf("failed to serve: %v", err)
	}
}
